package com.z1.core.web.util;

import com.z1.core.kvutils.KvFactory;
import com.z1.core.kvutils.KvObject;
import com.z1.core.web.dao.BaseMapper;

public class SafeUtil {

	public static final String SAFE_GROUP = "safe";
	private KvObject kvObject = KvFactory.initKvObject();
	
	public void set(String key, BaseMapper<?, ?> mapper) {
		set(SAFE_GROUP, key, mapper);
	}
	public void set(String group, String key, BaseMapper<?, ?> mapper) {
		if (null == group) {
			group = SAFE_GROUP;
		}
		long count = mapper.countAll();
		kvObject.set(group, key, count);
	}
	
	public Long get(String key) {
		return get(SAFE_GROUP, key);
	}
	public Long get(String group, String key) {
		if (null == group) {
			group = SAFE_GROUP;
		}
		Object obj = kvObject.get(group, key);
		if (null == obj) { return 0L; }
		try {
			return Long.parseLong(String.valueOf(obj));
		}
		catch (Throwable e) { }
		return 0L;
	}
	
}
